var util = require('./utils/util')
const Utils = require("./utils/uti.js");
var request = require('./utils/request.js');
App({
  onShow() {
    let that = this
  },
  onLaunch() {
    let that = this
    that.request = request

    Utils.checkSystemInfo(this);

    wx.authorize({
      scope: 'scope.record',
    });
  },



  // 未登录获取字典   // mobileUser/dictType/{dictType}
  //退出登录
  getDict(dictCode) {
    return new Promise((resolve, reject) => {
      var postObj = {
        url: '/mobileUser/dictType/' + dictCode,
        method: 'get'
      }
      request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          resolve(res)
        }
      })
    })
  },

  //后台字典
  getbackDict(dictCode) {
    return new Promise((resolve, reject) => {
      var postObj = {
        url: '/mobileUser/dictType/' + dictCode,
        method: 'get',

      }
      request.backWxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          resolve(res)
        }
      })
    })
  },
  // 回显数据字典
  selectDictLabel(datas, value) {
    var actions = [];
    Object.keys(datas).map((key) => {
      if (datas[key].dictValue == ('' + value)) {
        actions.push(datas[key].dictLabel);
        return false;
      }
    })
    return actions.join('');
  },
  logOut() {
    var postObj = {
      url: '/logout',
      data: '',
      method: 'post'
    }
    request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '成功退出!',
          icon: 'success'
        })
        request.removeToken('jy-token')
        request.removeToken('jy-bToken')
        this.toLoginIndexReLaunch()
      }
    })
  },

  logbackOut() {
    var postObj = {
      url: '/logout',
      data: '',
      method: 'post'
    }
    request.backWxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '成功退出!',
          icon: 'success'
        })
        request.removeToken('jy-token')
        request.removeToken('jy-bToken')
        this.toLoginIndexReLaunch()
      }
    })
  },
  getFrontInfo() {
    return new Promise((resolve, reject) => {
      var obj = {
        url: '/mobileUser/getInfo',
        data: '',
        method: 'get'
      }
      request.wxRequest(obj).then(res => {
        // console.log(res);

        if (res.data.code === 200) {
          resolve(res)
        } else {
          reject(res)
          this.toLoginReLaunch()
        }
      })
    })

  },
  getbackFrontInfo() {
    return new Promise((resolve, reject) => {
      var obj = {
        url: '/getInfo',
        data: '',
        method: 'get'
      }
      request.backWxRequest(obj).then(res => {

        if (res.data.code === 200) {
          resolve(res)
        } else {
          reject(res)
          this.toLoginReLaunch()
        }
      })
    })

  },
  // app全局数据
  globalData: {
    userInfo: null, //前端用户信息
    bUserInfo: null, //后端用户信息
    // token: wx.getStorageSync('jy-token'),
    // bToken: wx.getStorageSync('jy-bToken'),
  },
  //获取本地存储   有问题 不要用
  getStorage: function (Name) {
    // var data = null
    wx.getStorage({
      key: Name,
      success: (res) => {
        var data = JSON.parse(res.data)
        console.log(data);

        return data
      }
    })
    // data = JSON.parse(data)
    // console.log(data);
    // return data
  },
  //设置本地存储
  setStorage: function (Name, data) {
    wx.setStorage({
      key: Name,
      data: JSON.stringify(data)
    })
  },
  //移除本地存储    有问题 不要用
  removeStorage: function (Name) {
    wx.removeStorage({
      key: Name,
    })
  },
  //   localStorage.removeItem(key)
  // }
  // 可返回的跳转
  navigateTo(url) {
    wx.navigateTo({
      url: url,
    })
  },

  // 重定向到主页
  toLoginIndexReLaunch: function () {
    wx.reLaunch({
      url: '/pages/loginIndex/loginIndex',
    })
  },
  // 重定向到登录
  toLoginReLaunch: function () {
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },
  // 重定向到注册
  toRegisterReLaunch: function () {
    wx.reLaunch({
      url: '/pages/register/register',
    })
  },
  // 重定向到忘记密码
  toForgetReLaunch: function () {
    wx.reLaunch({
      url: '/pages/forget/forget',
    })
  },
  // 重定向到预约会见
  toAppointmentReLaunch: function () {
    wx.reLaunch({
      url: '/pages/appointment/appointment',
    })
  },
  //重定向到我的预约
  toMyAppointmentReLaunch: function () {
    wx.reLaunch({
      url: '/pages/myAppointment/myAppointment',
    })
  },
  //组成键值对方法
  transformObject: function (key, value) {
    var obj = {};
    obj[key] = value
    return obj;
  },
  globalData: {
    userInfo: null
  }
})